<template>
    <div :key="contentKey">
        <!-- :defaultSelected="{key:'xzid'}" -->
        <commpVxeTable
            :key="selectTemplateKey"
            :config="selectTemplateConfig" 
            :columns="selectTemplatecolumnsVxe" 
            :formItem="selectTemplateformItem"
            @radioChange="selectTemplateChange"
            :adaptiveHeight="true"
            ref="acceptanceRegistrationRef"
            
            @getDefaultSelected="getDefaultSelected"
        >
        </commpVxeTable>
    </div>
</template>
<script>
import commpVxeTable from '@/components/commp-vxe-table/commp-vxe-table.vue'
import assessCheckApi from './service/api'
import {  selectWjmbServe, commoninitWjjl,sjxdinitSjxdMb } from './service/index'
import blobType from '@/common/blobType'
export default {
    components:{
        commpVxeTable,
    },
    props:{
        fhbsxzt:{
            type:[String,Number],
            default: () => '',
            required:false
        },
    },
    data(){
        return{
            selectTemplateVisible:false,
            selectTemplateKey:null,
            templateId:'',
            takeNotesId:'',
            takeNotesType:'',
            selectTemplateConfig:{
                api:assessCheckApi.queryYwflWjmbList,
                rowId: 'id',
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                // height:520,
                autoHidden:true,
                pageSize:10,
                selectType:'radio',
                listData:'wjmbList',
                paramet:{
                }
            },
            selectTemplatecolumnsVxe:[{
                type:'radio',
                width:60,
            },{
                title: '模板标题', //列头显示文字
                field: 'fmbbt',
                key: 'fmbbt',
            },{
                title: '文件类型', //列头显示文字
                field: 'fwjlx',
                key: 'fwjlx',
            },{
                title: '模板类型1', //列头显示文字
                field: 'fmblx1',
                key: 'fmblx1',
            }],
            sqsxid:'',
            selectTemplateformItem:[{
                label: '模板标题',
                model: 'fmbbt',
            }],
            fhbsxzt:'',
            contentKey:null,
            parametData:{},
            okFunList:{
                sjxdinitSjxdMb:sjxdinitSjxdMb,
            },
            currentData:{},
        }
    },
    watch:{
        fhbsxzt: {
            handler (newval, oldVal) {
                //console.log('我在模板监听到什么值',newval)
            },
            immediate: true,
            deep: true
        },
    },
    mounted(){
        
    },
    methods:{
        //该组件用来初始化文件
        getSelectTemplate(row,val,sqsxid,fhbsxzt){
            this.fhbsxzt = fhbsxzt
            //console.log('我在选择模板',row,val,sqsxid)
            this.sqsxid = sqsxid
            this.templateId = ''
            // this.selectTemplateVisible = true
            if(sqsxid && typeof sqsxid !== 'string'){
                this.parametData = sqsxid
            }else{
                this.parametData.sqsxid = sqsxid
            }
            this.selectTemplateConfig = {
                api:fhbsxzt == 1?assessCheckApi.spcyRsWjmbqueryHbjcMbList:(row.listApi?assessCheckApi[row.listApi]:assessCheckApi.queryYwflWjmbList),
                rowId: 'id',
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                height:400,
                autoHidden:true,
                pageSize:100,
                selectType:'radio',
                listData:'wjmbList',
                paramet:{
                    
                    type:val,
                    ...this.parametData,
                }
            },
            //console.log('我在选择模板',row,val,sqsxid,fhbsxzt,this.selectTemplateConfig.api)
            this.selectTemplateKey = Date.now()
            this.takeNotesId = row.id
            this.currentData = row
            this.takeNotesType = val
            this.contentKey = Date.now()
        },
        getDefaultSelected(val){
            this.templateId = val
        },
        selectTemplateChange(val){
            var that = this
            this.$confirm({
                title:'您确认要选择当前模板吗？',
                onOk() {
                    that.templateId = val.row.id
                    that.selectTemplateHandleOk()
                },
                
            });
            
        },
        selectTemplateHandleOk(){
            //console.log(this.takeNotesType)
            // this.$emit('selectTemplateComplete',this.takeNotesType)
            if(this.templateId){
                let jkFun = this.currentData.okFun?this.okFunList[this.currentData.okFun]:commoninitWjjl
                let data = {}
                if(this.currentData.okFun == 'sjxdinitSjxdMb'){
                    data={
                        id:this.parametData.id,
                        fmbid:this.templateId,
                    }
                }else{
                    data = {fileid:this.takeNotesId,sqsxid:this.sqsxid,type:this.takeNotesType,id:this.templateId}
                }
                
                jkFun(data).then(res=>{
                    if(res.code == 200){
                        this.$emit('selectTemplateComplete',this.takeNotesType)
                        // this.manualSuccess()
                        this.$message.success('已选择模板')
                        this.templateId = ''
                        this.takeNotesId = ''
                        this.takeNotesType = ''
                        this.selectTemplateVisible = false
                        //console.log(this.takeNotesType)
                        
                    }else{
                        this.$message.warning(res.message)
                    }
                })
            }else{
                this.$message.warning('请选择模板')
            }
            
        },
    }
}
</script>